package com.intelligent.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.pojo.ProductionPlans;
import com.intelligent.service.ProductionPlansService;
import com.intelligent.mapper.ProductionPlansMapper;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author lenovo
* @description 针对表【production_plans(生产计划表)】的数据库操作Service实现
* @createDate 2025-09-04 08:41:34
*/
@Service
public class ProductionPlansServiceImpl extends ServiceImpl<ProductionPlansMapper, ProductionPlans>
    implements ProductionPlansService{

    @Override
    public IPage<ProductionPlans> selectProductionPlansWithUser(Page<ProductionPlans> page,
                                                               String planNo,
                                                               Integer planStatus,
                                                               Integer sourceType,
                                                               Integer urgentStatus,
                                                               Date startTime,
                                                               Date endTime,
                                                               String username) {
        return baseMapper.selectProductionPlansWithUser(page, planNo, planStatus, sourceType, 
                                                       urgentStatus, startTime, endTime, username);
    }

    @Override
    public boolean addProductionPlan(ProductionPlans productionPlans) {
        // 自动生成计划编号
        String planNo = generatePlanNo();
        productionPlans.setPlanNo(planNo);
        
        // 设置创建时间
        if (productionPlans.getCreateTime() == null) {
            productionPlans.setCreateTime(new Date());
        }
        
        // 设置默认值
        if (productionPlans.getPlanStatus() == null) {
            productionPlans.setPlanStatus(2); // 默认待下发工单
        }
        if (productionPlans.getWorkOrderCount() == null) {
            productionPlans.setWorkOrderCount(0);
        }
        if (productionPlans.getCompletedQuantity() == null) {
            productionPlans.setCompletedQuantity(0);
        }
        if (productionPlans.getOverallProductionSchedule() == null) {
            productionPlans.setOverallProductionSchedule("0%");
        }
        
        return baseMapper.insertProductionPlan(productionPlans) > 0;
    }

    @Override
    public boolean updateProductionPlanByPlanNo(ProductionPlans productionPlans) {
        return baseMapper.updateProductionPlanByPlanNo(productionPlans) > 0;
    }

    @Override
    public boolean deleteProductionPlanByPlanNo(String planNo) {
        return baseMapper.deleteProductionPlanByPlanNo(planNo) > 0;
    }

    @Override
    public ProductionPlans getProductionPlanByIdWithUser(Long id) {
        return baseMapper.selectProductionPlanByIdWithUser(id);
    }

    /**
     * 生成计划编号
     * @return 计划编号
     */
    private String generatePlanNo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timestamp = sdf.format(new Date());
        return "PP" + timestamp;
    }
}




